package cn.calm.xhtml.contentassist.autocomplete;
import static cn.calm.xhtml.XhtmlActivator.logInfo;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ContextInformation;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

import cn.calm.xhtml.entity.Attr;

public class AttrProcessorCompletionProposal implements ICompletionProposal,
		ICompletionProposalExtension {
	private String display;
	private Attr attr;
	private String insertString;
	private int length = -1;
	private Point point;

	public AttrProcessorCompletionProposal(Attr attr, String pattern) {
		this.attr = attr;
		display = attr.getName();
		StringBuffer app = new StringBuffer(getDisplayString());
		app.append("=\"\"");
		if (length == -1) {
			length = app.length() - pattern.length()- 2;
		}
		app.delete(0, pattern.length());
		insertString = app.toString();
	}

	@Override
	public void apply(IDocument arg0) {
		logInfo(arg0.get());
	}

	@Override
	public String getAdditionalProposalInfo() {
		return attr.getDesc();
	}

	@Override
	public IContextInformation getContextInformation() {
		logInfo("getContextInformation");
		return new ContextInformation("Context string", "Information string");
	}

	@Override
	public String getDisplayString() {
		return display;
	}

	@Override
	public Image getImage() {
		return null;
	}

	@Override
	public Point getSelection(IDocument arg0) {
		return point;
	}

	@Override
	public void apply(IDocument arg0, char arg1, int arg2) {
		String string = arg0.get();
		StringBuffer str = new StringBuffer(string);
		str.insert(arg2, insertString);
		point = new Point(arg2 + length, 0);
		arg0.set(str.toString());
	}

	@Override
	public int getContextInformationPosition() {
		logInfo("getContextInformationPosition");
		return 0;
	}

	@Override
	public char[] getTriggerCharacters() {
		return null;
	}

	@Override
	public boolean isValidFor(IDocument arg0, int arg1) {
		logInfo("isValidFor");
		return false;
	}

}
